package com.dfrz.household.service;/*
 * @创建者：黄苟苟
 * @创建时间：2021/8/12   14:14
 * @Description描述
 **/

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfrz.household.bean.ServerPerson;
import com.dfrz.household.mapper.ServerPersonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ServerPersonServiceImpl implements ServerPersonService {
    @Autowired
    ServerPersonMapper serPMapper;

    @Override
    public ServerPerson getUserById(Integer id) {
        return serPMapper.selectById(id);
    }

    @Override
    public List <ServerPerson> getUsers() {
        return serPMapper.selectList(null);
    }

    @Override
    public List <ServerPerson> getUsersParm(Map <String, String> parm) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (parm.get("uname") != null && !parm.get("uname").trim().equals("")) {
            queryWrapper.like("uname", parm.get("uname"));
        }
        if (parm.get("gender") != null && !parm.get("gender").trim().equals("")) {
            queryWrapper.eq("gender", parm.get("gender"));
        }
        return serPMapper.selectList(queryWrapper);
    }

    @Override
    public IPage <ServerPerson> getUserByPage(Map <String, String> parm, Integer pageNo, Integer pageSize) {
        IPage <ServerPerson> page = new Page <>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        if (parm.get("uname") != null && !parm.get("uname").trim().equals("")) {
            QueryWrapper queryWrapper = new QueryWrapper();
            ///queryWrapper.eq("uname",parm.get("uname"))
            queryWrapper.like("uname", parm.get("uname"));
            serPMapper.selectPage(page, queryWrapper);
        }

        return serPMapper.selectPage(page, null);
    }

    @Override
    public void delete(Integer id) {
        serPMapper.deleteById(id);
    }

    @Override
    public void save(ServerPerson serverPerson) {
        serPMapper.insert(serverPerson);
    }

    @Override
    public void update(ServerPerson user) {
        serPMapper.updateById(user);
    }

    @Override
    public ServerPerson getUserByUname(String sername) {
        ServerPerson serverPerson = serPMapper.getUserBysername(sername);
        return serverPerson;
    }

    @Override
    public List <ServerPerson> getAllSerperson() {
       List<ServerPerson> list=new ArrayList<ServerPerson>();
       list=serPMapper.getAllserverpersons();
       return list;
    }

    @Override
    public ServerPerson getOneServerPerson(Integer id) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("mid",id);
        ServerPerson serverPerson= serPMapper.selectOne(wrapper);
        return serverPerson;
    }

    @Override
    public ServerPerson getServerpersonCompanyById(Integer serid) {
        return serPMapper.getServerpersonCompanyById(serid);
    }

    @Override
    public ServerPerson getServerPersonById(Integer id) {
        return serPMapper.selectById(id);
    }

    @Override
    public List <ServerPerson> getServerpersonByPage(int currPage, int pageSize) {
        List<ServerPerson> list = new ArrayList<ServerPerson>();
        list=serPMapper.getAllserverpersons();

        //        从第几条数据开始
        int firstIndex = (currPage - 1) * pageSize;
//        到第几条数据结束
        int lastIndex = currPage * pageSize;
        return list.subList(firstIndex, lastIndex);
    }

}
